package com.zre.service.impl;

import com.alibaba.fastjson.JSON;
import com.zre.dao.BooklDao;
import com.zre.model.vo.BookInfo;
import com.zre.model.po.BookTypeCodePo;
import com.zre.model.po.CategoryPo;
import com.zre.service.BookInfoService;
import com.zre.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class BookInfoServiceImpl implements BookInfoService{

    @Autowired
    private BooklDao dao;

    /**
     * 查询书籍
     * @param bookType
     * @param bookName
     * @return
     */
    @Override
    public PageUtils queryBooksByCondition(String bookType, String bookName, Integer start, Integer end) {
        Set<Integer> ids = new HashSet<>();
        if (StringUtils.isEmpty(bookName) && !StringUtils.isEmpty(bookType)){
            ids.addAll(dao.queryBookId(bookType));
        }
        Integer count = dao.queryPageSize(bookType, bookName,ids);
        List<BookInfo> bookInfoList =  dao.queryByMultiConditions(bookType, bookName,ids,start,end);
        //所有书籍进行拼接所属类型
        bookInfoList.forEach(p-> p.setBookType(getByTypeCode(p.getTypeJson())));
        return new PageUtils(count, bookInfoList);
    }


    /**
     * 图书类型匹配
     * @param
     * @return
     */
    private List<CategoryPo> getByTypeCode(String codes) {

        List<CategoryPo> result = new LinkedList<>();

        List<CategoryPo> categoryPoList = getCategoryPoList();
        Set<String> codeSet = new HashSet<>(JSON.parseArray(codes,String.class));

        if (!CollectionUtils.isEmpty(categoryPoList))
        result = categoryPoList.stream().filter(p->codeSet.contains(p.getTypeCode())).collect(Collectors.toList());

        return result;
    }

    /**
     * 查询类型，用缓存处理一下
     * @param
     * @return
     */
    private List<CategoryPo> getCategoryPoList(){

        List<CategoryPo> categoryPoList = dao.queryBookType();
        return categoryPoList;
    }
    /**
     * 添加书籍
     * @param bookInfo
     * @return
     */
    @Override
    public int addBooks(BookInfo bookInfo) {

        List<String> typeCodes = new ArrayList<>();
        bookInfo.setCreatDate(new Date());
        if (!CollectionUtils.isEmpty(bookInfo.getBookType())){
            typeCodes = bookInfo.getBookType().stream().map(CategoryPo::getTypeCode).collect(Collectors.toList());
            bookInfo.setTypeJson(JSON.toJSONString(typeCodes));
        }

        if (dao.insertBook(bookInfo) == 1 && !CollectionUtils.isEmpty(typeCodes)){
            Integer bookId = bookInfo.getBookId();
            for(String p :typeCodes)
                addBookTypeCode(new BookTypeCodePo(bookId, p));
        }

        return 0;
    }
    /**
     * 添加书籍类型中间表信息
     * @param bookTypeCodePo
     * @return
     */
    @Override
    public int addBookTypeCode(BookTypeCodePo bookTypeCodePo) {

        return dao.insertBookTypeCode(bookTypeCodePo);
    }



}
